<?php
namespace System\Controller;
use Common\Controller\BaseController;
class SysAdminController extends BaseController{
	const modelName = CONTROLLER_NAME;
	
	protected function _initialize(){
		parent::_initialize();
		
		$this->checkAuth();
		
		$this->model = D(self::modelName);
	}
	
	public function index(){
        if(IS_AJAX){
            //排序
            $order = I('get.order');

            //获取列表
            $field = 'pk_id,(SELECT name FROM sys_role WHERE pk_id = fk_role_id) AS role_name,nickname,email,create_time';
            $data = $this->getRows(self::modelName, $map ,$field ,1 ,$order);
            $this->data = $data;
            $this->rtnResult(0);
        }else{
            //表头、字段名
            $data['field'] = 'pk_id,role_name,nickname,email,create_time';
            $data['titleArr'] = array('编号','所属角色','昵称','邮箱','添加时间');
            $this->showView($data,'./Public/Admin/Tpl/index.html');
        }
	}
	
	
    /**
     * 新增数据
     */
    public function add(){
    	if (IS_POST){
            $encrypt = I('encrypt');
            if(!$encrypt){
                unset($_POST['encrypt']);
            }
    		$this->addData($this->model,'添加成功|添加失败','index');
        }else {
    		$field = 'pk_id,name';
    		$data['role_rows'] = $this->getRows('sys_role', '' ,$field);
			$this->showView($data);  		
    	}
    }
    
    /**
     * 删除数据
     * POST请求为批删，GET请求为删除单条
     */
//    public function del(){
//		$this->delData($this->model,'删除成功|删除失败|ajax');
//    }

	/**
	 * 编辑数据
	 * Enter description here ...
	 * @param unknown_type $success_url 修改成功后跳转地址
	 */
    public function edit($success_url=''){
    	if (IS_POST){
    		//更新操作
            $encrypt = I('encrypt');
            $psd = I('password');
            if(!$encrypt || !$psd){
                unset($_POST['encrypt']);
                unset($_POST['password']);
            }else{
                $_POST['password'] = strengthen_md5($_POST['password'],$_POST['encrypt']);
            }
    		$this->editData($this->model,'修改成功|修改失败','index');
    	}else {
    		//获取详细信息
    		$map['pk_id'] = I('pk_id');
    		$data['info'] = $this->getDataInfo($this->model, $map);
    		
    		//获取角色列表
    		$field = 'pk_id,name';
    		$data['role_rows'] = $this->getRows('SysRole', '' ,$field);
    		$this->showView($data);
    	}
    }
    
    
    /**
     * 修改密码
     * Enter description here ...
     */
    public function modifyPwd(){
    	if (IS_POST){
            $old = I('old');$password = I('password');$repassword = I('repassword');
            !$old && $this->rtnResult(1,'请输入旧密码');
            !$password && $this->rtnResult(1,'请输入新密码');
            !$repassword && $this->rtnResult(1,'请输入确认密码');
            ($password != $repassword) && $this->rtnResult(1,'两次密码输入不一致');
            ($password == $old) && $this->rtnResult(1,'新旧密码不能相同');

            //获取旧密码
            $admin_info = getAdminInfo();
            if(strengthen_md5($old,$admin_info['encrypt']) != $admin_info['password']){
                $this->rtnResult(1,'旧密码不正确，请确认后输入');
            }

            //修改密码
            $result = $this->model->where('pk_id = %d',$admin_info['pk_id'])->setField('password',strengthen_md5($password,$admin_info['encrypt']));
            if($result === false){
                $this->rtnResult(1,'修改失败');
            }else{
                $this->rtnResult(0,'修改成功');
            }
        }else {
    		$this->showView();
    	}
    }
    
    
	/**
     * 修改个人信息
     * Enter description here ...
     */
    public function modifySelfInfo(){
    	if (IS_POST){
            $aid = getAdminId();
            $map['pk_id'] = $aid;
            $result = $this->editData(D(self::modelName),'|修改失败','',$map);
            if($result){
                //重新生成令牌
                $info = M(self::modelName)->where('pk_id = %d',$aid)->find();
                createAdminToken($info);
            }
            $this->rtnResult(1,'修改成功'.$result);
    	}else {
    		$this->showView();
    	}
    }
}
?>